//
//  H_FindViewController.m
//  Honey
//
//  Created by 贾亚涵 on 16/3/17.
//  Copyright © 2016年 贾亚涵. All rights reserved.
//

#import "H_TimeViewController.h"
#import "H_TimeDisplayView.h"
#import "H_TimeHeaderView.h"
#import "H_TimeCell.h"
#import "H_TimeWordViewController.h"

@interface H_TimeViewController ()<UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    UIImage * _leftBarBtnImage;
    UIImage * _rightBarBtnImage;
    BOOL BtnRightOrLeft;
    H_TimeDisplayView * _timeView;
    
    UITableView * _tableView;
    H_TimeHeaderView * _headerView;
    
    UIView * _qqView;
    BOOL tag;
    UIButton * _addWordBtn;
    UIButton * _addPicBtn;
    UIImageView * _wordImageView;
    UIImageView * _picImageView;
    UILabel * _wordLabel;
    UILabel * _picLabel;
    
    H_ScrollFuctionV * _bannerView;
    
}

@end

@implementation H_TimeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self creatTimeWidget];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
}

//创建页面上的控件（widget）
-(void)creatTimeWidget
{
    _timeView = [H_TimeDisplayView new];
    
    
    //添加navigation的左右button
    [self navigationBarBtn];
    //初始化tableview
    [self initTableView];
    //初始化滚动栏
    [self initBanner];
    
    [self initQQView];
}


//给navigationBar添加左Button
-(void)navigationBarBtn
{
    _leftBarBtnImage = [[UIImage imageNamed:@"Time_Lbtn1"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:_leftBarBtnImage style:UIBarButtonItemStyleDone target:self action:@selector(leftBarBtnClicked:)];
    
    _rightBarBtnImage = [[UIImage imageNamed:@"Time_Rbtn"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:_rightBarBtnImage style:UIBarButtonItemStyleDone target:self action:@selector(rightBarBtnClicked:)];
}

//点击事件
-(void)leftBarBtnClicked:(id)sender
{
    if (sender && [sender isKindOfClass:[UIBarButtonItem class]])
    {
        if (BtnRightOrLeft)
        {
            _leftBarBtnImage = [[UIImage imageNamed:@"Time_Lbtn1"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            
            self.navigationItem.leftBarButtonItem.image = _leftBarBtnImage;
            
            [_timeView removeFromSuperview];
            
            BtnRightOrLeft = NO;
        }else
        {
            _leftBarBtnImage =[[UIImage imageNamed:@"Time_Lbtn2"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            
            self.navigationItem.leftBarButtonItem.image = _leftBarBtnImage;
            
            _timeView.frame = CGRectMake(0,0 , Screen_Width, Screen_Height-64);
            
            [self.view.superview addSubview:_timeView];
            
            BtnRightOrLeft = YES;
        }
    }
}

-(void)rightBarBtnClicked:(id)sender
{
    if (sender && [sender isKindOfClass:[UIBarButtonItem class]])
    {
        if (tag)
        {
            [_qqView removeFromSuperview];
            
            tag = NO;
        }else
        {
            [self.view.superview addSubview:_qqView];
            tag = YES;
        }
    }
}

-(void)initTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, Screen_Width, Screen_Height)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator =NO;
    _tableView.rowHeight = 300;
    
    _headerView = [[H_TimeHeaderView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, 200)];
    _tableView.tableHeaderView = _headerView;
    
    [self.view addSubview:_tableView];
}

#pragma mark- UITableViewDelegate


#pragma mark- UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 7;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellType =@"cellType";
    
    //通过Identifier找cell
    H_TimeCell * cell = [tableView dequeueReusableCellWithIdentifier:cellType];
    
    if (cell == nil)
    {
        //从xib中创建cell
        cell = (H_TimeCell *)[[[NSBundle mainBundle] loadNibNamed:@"H_TimeCell" owner:self options:nil] lastObject];
    }
    
    return cell;
    
}

#pragma mark- initBanner
-(void)initBanner
{
    NSMutableArray * _imageMutableArray = [[NSMutableArray alloc] init];
    
    for (int index = 1; index<6; index++)
    {
        NSString * imageName = [[NSString alloc] initWithFormat:@"TimeHrader%d",index];
        
        [_imageMutableArray addObject:imageName];
    }
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    _bannerView = [H_ScrollFuctionV showScrollFuction:HWScrollViewTypeBanner ImageArray:_imageMutableArray];
    
    [_headerView addSubview:_bannerView];
    
}

//初始化创建说说的视图
-(void)initQQView
{
    _qqView = [[UIView alloc] initWithFrame:CGRectMake(Screen_Width/2, 64, Screen_Width/2-10, 150)];
    _qqView.backgroundColor = [UIColor colorWithRed:255/255.0f green:223/255.0f blue:223/255.0f alpha:1];
    
    _addWordBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, Screen_Width/2-10, 75)];
    [_addWordBtn addTarget:self action:@selector(AddWordBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [_qqView addSubview:_addWordBtn];
    _wordImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 20, 35, 35)];
    _wordImageView.backgroundColor = [UIColor clearColor];
    _wordImageView.image = [UIImage imageNamed:@"H_TimeWordTalk"];
    [_addWordBtn addSubview:_wordImageView];
    _wordLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_wordImageView.frame)+20, 15, 100, 45)];
    _wordLabel.backgroundColor = [UIColor clearColor];
    _wordLabel.text = @"添加文字";
    _wordLabel.font = [UIFont systemFontOfSize:20];
    [_addWordBtn addSubview:_wordLabel];
    
    _addPicBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_addWordBtn.frame), Screen_Width/2-10, 75)];
    [_addPicBtn addTarget:self action:@selector(AddPicBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [_qqView addSubview:_addPicBtn];
    _picImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 20, 35, 35)];
    _picImageView.backgroundColor = [UIColor clearColor];
    _picImageView.image = [UIImage imageNamed:@"H_TimePhotoTalk"];
    [_addPicBtn addSubview:_picImageView];
    _picLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_picImageView.frame)+20, 15, 100, 45)];
    _picLabel.backgroundColor = [UIColor clearColor];
    _picLabel.text = @"添加图片";
    _picLabel.font = [UIFont systemFontOfSize:20];
    [_addPicBtn addSubview:_picLabel];

}

-(void)AddWordBtnClicked:(id)sender
{
    if (sender && [sender isKindOfClass:[UIButton class]])
    {
        H_TimeWordViewController * word = [H_TimeWordViewController new];
        [self.navigationController pushViewController:word animated:YES];
    }
}

-(void)AddPicBtnClicked:(id)sender
{
    if (sender && [sender isKindOfClass:[UIButton class]])
    {
        UIAlertController * sheetView = [UIAlertController alertControllerWithTitle:@"选择操作" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        
        UIAlertAction * cancleAction = [UIAlertAction actionWithTitle:@"取消操作" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction * cameraAction = [UIAlertAction actionWithTitle:@"拍摄照片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
                [self takePhoto];
        }];
        
        UIAlertAction * photoAction = [UIAlertAction actionWithTitle:@"从照片中选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [self localPhoto];
        }];
        
        [sheetView addAction:cancleAction];
        [sheetView addAction:cameraAction];
        [sheetView addAction:photoAction];
        [self presentViewController:sheetView animated:YES completion:nil];
        

    }
}

-(void)takePhoto
{
    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    
    picker.delegate = self;
    
    picker.allowsEditing = YES;
    
    [self presentViewController:picker animated:YES completion:^{
        
    }];
}

-(void)localPhoto
{
    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    picker.delegate = self;
    
    [self presentViewController:picker animated:YES completion:^{
        
    }];
    
}

#pragma mark- UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
    
    UIImage * image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    
    if (image)
    {
        
    }
}

@end
